import { Module } from '@nestjs/common'
import { LogsService } from './logs.service'
import { LogsController } from './logs.controller'
import { TypeOrmModule } from '@nestjs/typeorm'
import { Logs } from './logs.entity'
import { ConfigService } from '@nestjs/config'
import * as winston from 'winston'
import { WinstonModule, utilities as nestWinstonModuleUtilities } from 'nest-winston'

import 'winston-daily-rotate-file'
@Module({
  imports: [
    TypeOrmModule.forFeature([Logs]),
    WinstonModule.forRootAsync({
      inject: [ConfigService],
      useFactory: () => {
        const myFormat = winston.format.printf(({ level, message, timestamp, stack }) => {
          return `${timestamp} [] ${level}: ${message} : ${stack}`
        })
        const ConsoleTransport = new winston.transports.Console({
          format: winston.format.combine(
            winston.format.timestamp(),
            winston.format.ms(),
            nestWinstonModuleUtilities.format.nestLike('MyApp', {
              colors: true,
              prettyPrint: true
            })
          )
        })
        const DailyRotateFileTransport = new winston.transports.DailyRotateFile({
          filename: 'logs/logs-%DATE%.log',
          datePattern: 'YYYY-MM-DD',
          zippedArchive: true,
          maxSize: '20m',
          maxFiles: '14d',
          format: winston.format.combine(winston.format.timestamp(), winston.format.ms(), myFormat)
        })
        return {
          transports: [ConsoleTransport, DailyRotateFileTransport]
        }
      }
    })
  ],
  exports: [LogsService],
  providers: [LogsService],
  controllers: [LogsController]
})
export class LogsModule {}
